package com.example.shop.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shop.R;
import com.example.shop.entity.HomeEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.util.List;

public class ItemLvClasslistHomeFragAdapter extends BaseAdapter {

    private List<HomeEntity.HomeClasslistEntity> mEntities;
    private DisplayImageOptions mOptions;
    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvClasslistHomeFragAdapter(Context context, List<HomeEntity.HomeClasslistEntity> entities) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mEntities = entities;
        mOptions = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.product_loading)
                .showImageOnFail(R.drawable.product_loading)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();
    }

    @Override
    public int getCount() {
        return mEntities.size();
    }

    @Override
    public HomeEntity.HomeClasslistEntity getItem(int position) {
        return mEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_classlist_home_frag, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((HomeEntity.HomeClasslistEntity) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(HomeEntity.HomeClasslistEntity entity, ViewHolder holder) {
        //TODO implement
        holder.tvItemLvClasslistHomeFrag.setText(entity.getTitle());
        ImageLoader.getInstance().displayImage(entity.getPic(),holder.imgvItemLvClasslistHomeFrag,mOptions);
    }

    protected class ViewHolder {
        private ImageView imgvItemLvClasslistHomeFrag;
        private TextView tvItemLvClasslistHomeFrag;

        public ViewHolder(View view) {
            imgvItemLvClasslistHomeFrag = (ImageView) view.findViewById(R.id.imgv_item_lv_classlist_home_frag);
            tvItemLvClasslistHomeFrag = (TextView) view.findViewById(R.id.tv_item_lv_classlist_home_frag);
        }
    }
}
